<?php

class UploadController extends Zend_Controller_Action 
{
    protected $user;
    protected $group;
    protected $marker;
    
    public function indexAction()
    {
        $request = $this->getRequest();
        $data = $request->getRawBody();        
        if(strlen($data) > 0){
            $path = 'uploads\\';
            $fname = date('d-m-Y-H-i-s').'.xml';            
            $fp = fopen($path.$fname, "w");
            $this->saveXML($data);
            fwrite($fp, $data);        
            fclose($fp);        
            $this->view->result = 'Upload success';
        }else{
            $this->view->result = 'Invalid request';
        }
    }
    public function saveXML($data){
        $xml = simplexml_load_string($data);
        $user = $this->getUser();
        $user_id = $user->saveUser($xml->Email);        
        $group = $this->getGroup();        
        $group_id = $group->saveGroup($xml->Group['name'], $user_id);        
        $marker = $this->getMarker();
        $marker->saveMarkers($xml->Group, $group_id);
    }
    public function loadAction(){
        $fname = 'uploads\data.txt';
        $fp = fopen($fname, "r");
        $data = fread($fp, filesize($fname));
        $xml = simplexml_load_string($data);
        $user = $this->getUser();
        $user_id = $user->saveUser($xml->Email);
        echo "User Id: ".$user_id;
        $group = $this->getGroup();        
        $group_id = $group->saveGroup($xml->Group['name'], $user_id);
        echo 'Group Id: '.$group_id;
        $marker = $this->getMarker();
        $marker->saveMarkers($xml->Group, $group_id);
        echo "Saved";
        $this->view->xml = $xml;
    }
    public function fileAction()
    {
        $request = $this->getRequest();
        $form    = $this->_getGuestbookForm();

        if ($this->getRequest()->isPost()) {
            
            if ($form->isValid($request->getPost())) {
                
                $model = $this->_getModel();
                $model->save($form->getValues());
                return $this->_helper->redirector('index');
            }
        }
        
        $this->view->form = $form;
    }

    protected function getUser()
    {
        if (null === $this->user) {
            require_once APPLICATION_PATH . '/models/User.php';
            $this->user = new Model_User();
        }
        return $this->user;
    }
    
    protected function getGroup()
    {
        if (null === $this->group) {
            require_once APPLICATION_PATH . '/models/Group.php';
            $this->group = new Model_Group();
        }
        return $this->group;
    }
    
    protected function getMarker()
    {
        if (null === $this->marker) {
            require_once APPLICATION_PATH . '/models/Marker.php';
            $this->marker = new Model_Marker();
        }
        return $this->marker;
    }

    protected function _getGuestbookForm()
    {
        require_once APPLICATION_PATH . '/forms/User.php';
        $form = new Form_User();
        $form->setAction($this->_helper->url('sign'));
        return $form;
    }
}
